<template>
  <el-dialog v-model="visible" fullscreen destroy-on-close @close="handleClose" draggable :loading="loading">
    <template #header>{{ TitleMap[dialogStatus] }}</template>
    <el-form :rules="rules" ref="formRef" :model="modelForm" label-width="100px">
      <el-row :gutter="40">
        <el-col :span="18">
          <el-form-item label="标题" prop="title">
            <el-input v-model="modelForm.title" />
          </el-form-item>
          <el-form-item label="简介" prop="description">
            <el-input v-model="modelForm.description" type="textarea" :rows="2" placeholder="请输入简介" />
          </el-form-item>
          <el-form-item label="内容" prop="content">
            <Tinymce :key="tinymceKey" v-model:value="modelForm.content" style="width: 100%" placeholder="请输入内容" />
          </el-form-item>
        </el-col>

        <el-col :span="6">
          <el-collapse v-model="activeCollapse">
            <el-collapse-item title="文章状态" name="1">
              <el-checkbox v-model="modelForm.status" :true-label="2" :false-label="1">保存草稿</el-checkbox>
            </el-collapse-item>

            <el-collapse-item title="文章类型" name="2">
              <el-row :gutter="20">
                <el-col :span="12">
                  <MyModel :list="modelForm.modelData" :all="false" more-value v-model="modelForm.modelid" @onChange="onModelChange" style="width: 100%" />
                </el-col>
                <el-col :span="12">
                  <el-form-item prop="catid" label-width="0">
                    <el-select v-model="modelForm.catid" style="width: 100%" placeholder="请选择类别">
                      <el-option v-for="item in state.currentCategories" :key="item.id" :label="item.colTitle" :value="item.id" />
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-collapse-item>
            <el-form-item label-width="0" class="pt-5">
              <el-button @click="handleClose">取消</el-button>
              <el-button type="primary" @click="handleSubmit()">确认</el-button>
            </el-form-item>
          </el-collapse>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="handleSubmit()">确认</el-button>
    </template>
  </el-dialog>
</template>
<script lang="ts" setup>
import useLoading from '@/hooks/loading'
import { TitleMap } from '@/config'
import Tinymce from '@/components/Editor/tinymce.vue'

interface EditProps {
  modelForm: any
  editVisible: boolean
  createData: any
  updateData: any
  setEditVisible: any
  models: any
  title?: string
  dialogStatus: string
}
const state = reactive<any>({
  categories: [],
  currentCategories: [],
})
const props = withDefaults(defineProps<EditProps>(), {
  editVisible: false,
  modelForm: {
    ord: 0,
    title: '',
    description: '',
  },
  models: [],
  title: '',
  dialogStatus: 'create',
  createData: () => {},
  updateData: () => {},
  setEditVisible: () => {},
})
const activeCollapse = ['1', '2', '3']
const { modelForm, models } = toRefs(props)
const { loading, setLoading } = useLoading()
const isEdit = computed(() => props.dialogStatus === 'update')
const formRef = ref<any>(null)
const tinymceKey = ref(1)
const visible = ref(false)
const rules = {
  name: [{ required: true, message: '请输入分类名称', trigger: 'blur' }],
}

const filterCategory = (val: any) => {
  const cur = models.value.filter((item: any) => item.modelid === +val)

  if (cur) {
    state.currentCategories = cur
  }
}

const init = () => {
  filterCategory(modelForm.value.modelid)
  modelForm.value.modelid = { modelid: modelForm.value.modelid }
}

watchEffect(() => {
  isEdit.value && init()
})

watch(
  () => props.editVisible,
  (show) => {
    visible.value = show
  }
)
const onModelChange = (value: any) => {
  filterCategory(value.modelid)
  modelForm.value.catid = null
}

const handleClose = () => {
  props.setEditVisible()
  formRef.value!.resetFields()
}

const handleModelForm = (modelForm: any) => {
  delete modelForm.value.modelid
  delete modelForm.value.modelData

  return modelForm
}

const handleSubmit = () => {
  formRef.value
    .validate((valid: any) => {
      if (!valid) return

      const data = unref(handleModelForm(modelForm))
      props.dialogStatus === 'create' ? props.createData(data) : props.updateData(data)
      handleClose()
    })
    .catch((error: any) => {
      console.log('error', error)
    })
    .finally(() => {
      setLoading(false)
    })
}
</script>
<style lang="scss"></style>
